package com.ai.web.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ai.web.service.WxUserService;
import com.ai.web.vo.ResultModel;
import com.ai.web.vo.WxUserV;
import com.tiny.persistence.domain.WxUser;
import com.tiny.persistence.repository.WxUserRepo;
/**
 * 微信用户
* @ClassName: WxUserServiceImpl 
* @Description: TODO 
* @author:  
* @date: 2014年3月28日
*
 */
@Service("wxUserService")
public class WxUserServiceImpl extends BaseServiceImpl<WxUser, Integer, WxUserV> implements WxUserService  {

	@Autowired
	private WxUserRepo wxUserRepo;
	
	@Autowired
	public WxUserServiceImpl(WxUserRepo wxUserRepo) {
		super(wxUserRepo);
	}

	public WxUserV getWxUser(int userId) throws Exception {
		return getT(userId);
	}

	public ResultModel saveWxUser(WxUserV wxUserV) throws Exception {
		
		WxUserV saveV = saveT(wxUserV);
		
		ResultModel rm = new ResultModel();
		rm.setData(saveV);
		rm.setSuccessCode();
		
		return rm;
	}

	public ResultModel deleteWxUsers(List<Integer> userIds) throws Exception {
		deleteTs(userIds);
		
		ResultModel rm = new ResultModel();
		rm.setSuccessCode();
		rm.setMsg("删除成功！");
		
		return rm;
	}

	public WxUser getByOpenId(String openId) {
		return wxUserRepo.getByOpenId(openId);
	}
	
	public WxUserV getWxUserByOpenId(String openId) throws Exception {
		WxUser wxUser = wxUserRepo.findByOpenId(openId);
		if (wxUser == null) {
			return null;
		} else {
			WxUserV wxUserV = new WxUserV();
			BeanUtils.copyProperties(wxUser, wxUserV);
			
			return wxUserV;
		}
	}

	public ResultModel existsByOpenId(String openId) throws Exception {
		WxUserV wxUserV = getWxUserByOpenId(openId);
        ResultModel rm = new ResultModel();
		if(wxUserV==null) {
			rm.setSuccessCode();
		}else {
			rm.setFailCode();
		}
        return rm;
	}

	public WxUserV getByMobilePhone(String bankUserPhone) {
		WxUser wxUser = wxUserRepo.findByMobilePhone(bankUserPhone);
		if (wxUser == null) {
			return null;
		} else {
			WxUserV wxUserV = new WxUserV();
			BeanUtils.copyProperties(wxUser, wxUserV);
			return wxUserV;
		}
	}
	
}
